//---------------------------------------------------------------------------
#pragma hdrstop
#include "TRosterHandler.h"
#include <stdio.h>
#pragma package(smart_init)
//---------------------------------------------------------------------------
TRosterHandler::TRosterHandler(Client *client)
{
   if(NULL==client)return;
   _client=client;
   _client->rosterManager()->registerRosterListener(this);
}
//---------------------------------------------------------------------------
TRosterHandler::~TRosterHandler()
{
   if(NULL==_client)return;
   _client->rosterManager()->removeRosterListener();
}
//---------------------------------------------------------------------------
void TRosterHandler::handleItemSubscribed( const JID& jid )
{
	if(FOnItemSubscribed)FOnItemSubscribed(jid);
}
//---------------------------------------------------------------------------
void TRosterHandler::handleItemAdded( const JID& jid)
{
	if(FOnItemAdded)FOnItemAdded(jid);
}
//---------------------------------------------------------------------------
void TRosterHandler::handleItemUnsubscribed( const JID& jid )
{
	 if(FOnItemUnsubscribed)FOnItemUnsubscribed(jid);
}
//---------------------------------------------------------------------------
void TRosterHandler::handleItemRemoved( const JID& jid )
{
	if(FOnItemRemoved)FOnItemRemoved(jid);
}
//---------------------------------------------------------------------------
void TRosterHandler::handleItemUpdated( const JID& jid )
{
	if(FOnItemUpdated)FOnItemUpdated(jid);
}
//---------------------------------------------------------------------------
void TRosterHandler::handleRoster( const Roster& roster )
{
	if(FOnRosterItem==NULL)return;
	char groupname[255]="";
	char resname[255]="";
	try{
		Roster::const_iterator it = roster.begin();
		  for( ; it != roster.end(); ++it )
		  {
            if((*it).second->name()==EmptyString)continue;
			StringList g = (*it).second->groups();
			StringList::const_iterator it_g = g.begin();
			for( ; it_g != g.end(); ++it_g )
			  sprintf(groupname,"\tgroup: %s\n", (*it_g).c_str() );
			RosterItem::ResourceMap::const_iterator rit = (*it).second->resources().begin();
			for( ; rit != (*it).second->resources().end(); ++rit )
			  sprintf(resname,"resource: %s\n", (*rit).first.c_str() );
			  FOnRosterItem((*it).second->jid(),(*it).second->name(),groupname,resname,(*it).second->subscription(),(*it).second->online());
		  }
	  }catch(...){}
}
//---------------------------------------------------------------------------
void TRosterHandler::handleRosterError( const IQ& iq )
{
	if(FOnRosterError)FOnRosterError(iq);
}
//---------------------------------------------------------------------------
void TRosterHandler::handleRosterPresence( const RosterItem& item, const std::string& resource,
						   Presence::PresenceType presence, const std::string& msg )
{
	if(FOnRosterPresence)FOnRosterPresence(item,resource,presence,msg);
}
//---------------------------------------------------------------------------
void TRosterHandler::handleSelfPresence( const RosterItem& item, const std::string& resource,
						   Presence::PresenceType presence, const std::string& msg )
						   {
	 if(FOnSelfPresence)FOnSelfPresence(item,resource,presence,msg);
}
//---------------------------------------------------------------------------
bool TRosterHandler::handleSubscriptionRequest( const JID& jid, const std::string& msg )
{
	 if(FOnSubscriptionRequest)return FOnSubscriptionRequest(jid,msg);
}
//---------------------------------------------------------------------------
bool TRosterHandler::handleUnsubscriptionRequest( const JID& jid, const std::string& msg )
{
	 if(FOnUnsubscriptionRequest)return FOnUnsubscriptionRequest(jid,msg);
}
//---------------------------------------------------------------------------
void TRosterHandler::handleNonrosterPresence( const Presence& presence )
{
	 if(FOnNonrosterPresence)FOnNonrosterPresence(presence);
}
//---------------------------------------------------------------------------
